import { Global } from '../Global';
import { Animation } from 'cc';
import { Entity } from './def/Entity';

export class DoorEntity extends Entity {

    handleEvent(dirct?: string) {
        Global.heroManage.globalMoveKey = true
        console.log('open door')
        const state = {
            move: false,
            cartoon: false,
        }

        // 检查钥匙
        const keys = Global.hero._heroState.keys[this.type]
        if (keys === null || keys === undefined || keys <= 0) {
            Global.popUtil.popUp('您没有钥匙')
            Global.heroManage.globalMoveKey = false
            return state
        }

        Global.hero._heroState.keys[this.type] -= 1
        this.node.getComponent(Animation).play('yellow')
        setTimeout(() => {
            Global.em.removeEntity(Global.fm.getFloor(), this)
            Global.heroManage.globalMoveKey = false
            switch (dirct) {
                case ('up'): {
                    Global.heroManage.up()
                    break
                }
                case ('down'): {
                    Global.heroManage.down()
                    break
                }
                case ('left'): {
                    Global.heroManage.left()
                    break
                }
                case ('right'): {
                    Global.heroManage.right()
                    break
                }
            }
        }, 350)
        return state
    }

    preCheck() {
        // 检查钥匙
        const keys = Global.hero._heroState.keys[this.type]
        if (keys === null || keys === undefined || keys <= 0) {
            Global.popUtil.popUp('您没有钥匙')
            return false
        }
        return true
    }

}

